package com.haier.dreambox.demo.utils;


import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.text.TextUtils;
import android.view.View;
import com.haier.dreambox.demo.activity.HomeActivity_;

/**
 * Description:显示指定组件的对话框,并跳转至指定的Activity
 *
 * @author maoyun0903@163.com
 * @version 1.0
 */
public class DialogUtil {


    public static void showDialog(final Context ctx
            , String msg, boolean goHome) {
        // 创建一个AlertDialog.Builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx)
                .setMessage(msg).setCancelable(false);
        if (goHome) {
            builder.setPositiveButton("确定", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent i = new Intent(ctx, HomeActivity_.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    ctx.startActivity(i);
                }
            });
        } else {
            builder.setPositiveButton("确定", null);
        }
        builder.create().show();
    }

    public static void showDialog(Context ctx
            , String msg, String positiveMsg, OnClickListener positiveListener
            , String negativeMsg, OnClickListener negativeListener
    ) {
        // 创建一个AlertDialog.Builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx)
                .setMessage(msg).setCancelable(false);
        if (!TextUtils.isEmpty(positiveMsg)) {
            builder.setPositiveButton(positiveMsg, positiveListener);
        }
        if (!TextUtils.isEmpty(negativeMsg)) {
            builder.setNegativeButton(negativeMsg, negativeListener);
        }
        builder.create().show();
    }

    // 定义一个显示指定组件的对话框
    public static void showDialog(Context ctx, View view) {
        new AlertDialog.Builder(ctx)
                .setView(view).setCancelable(false)
                .setPositiveButton("确定", null)
                .create()
                .show();
    }

    public static ProgressDialog buildProgressDialog(Context context, String msg, final Runnable cancelRunnable) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(msg);
        progressDialog.setCanceledOnTouchOutside(false);
        if (cancelRunnable != null)
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    cancelRunnable.run();
                }
            });
        return progressDialog;
    }
}